/* jQuery File Tree Plugin++
*
* Version 1.01
*
* modify by collonn@126.com 2010-08-03
*
* Usage: $('.fileTreeDemo').fileTree( options);
* 
* Defaults:
* id : the div that you binded
* root : just use it on init tree
* script : the remote url that data come from
* isFirst : is or not init tree
* expandAll : is or not expand all parent node
* showLeaf : is or not show the leaf node
* showCheckbox : is or not show checkbox
* complete : when tree loaded successfully completed, the function will execute
* 
* History:
* 1.01 - add expandAll, showLeaf, showCheckbox options, and work well on ie6, firefox3.6.8, chrome5.0, opera10.60
* 1.00 - just modify from jqueryfiletree sourcecode
* 
* The Example For The Return Value From Server
* <ul style="" class="jqueryFileTree">
* 	<li class="directory expanded">
* 		<a rel="2/" href="#">食品</a>
* 		<ul style="display: block;" class="jqueryFileTree">
* 			<li class="directory expanded">
* 				<a rel="6/" href="#">肉类</a>
* 				<ul style="display: block;" class="jqueryFileTree">
* 					<li class="file ext_jsp leaf">
* 						<a rel="7" href="#">猪肉</a>
* 					</li>
*				</ul>
*			</li>
*
*			<li class="directory expanded">
*				<a rel="8/" href="#">蔬菜类</a>
*				<ul style="display: block;" class="jqueryFileTree">
*					<li class="file ext_jsp leaf">
*						<a rel="9" href="#">白菜</a>
*					</li>
*				</ul>
*			</li>
*		</ul>
*	</li>
*
*	<li class="directory expanded">
*		<a rel="3/" href="#">电器</a>
*		<ul style="display: block;" class="jqueryFileTree">
*			<li class="file ext_jsp leaf">
*				<a rel="4" href="#">电视机</a>
*			</li>
*
*			<li class="file ext_jsp leaf">
*				<a rel="5" href="#">电冰箱</a>
*			</li>
*		</ul>
*	</li>
*</ul>
*/
(function($) {
	
	jQuery.fn.fileTree = function(options){
		defaults = {
			id: '#' + $(this).attr('id'),
			root: '',
			script: '',
			expandSpeed: 500,
			collapseSpeed: 500,
			expandEasing: 'swing',
			collapseEasing: 'swing',
			loadMessage: 'loading...',
			isFirst: true,
			expandAll: false,
			showLeaf: true,
			showCheckbox: false,
			onlyOneCanChecked: false,
			onlyDeepInCanChecked: false,
			activeHref: true,
			complete: null,
			isIE6: $.browser.msie && parseInt($.browser.version) == 6 ? true : false
		};
		
		jQuery.extend(defaults,options);
		
		if(defaults.script == ''){
			alert('To User jQueryFileTree\nYou Must Confirm Root and Script Not Null');
			return;
		}
		
		//折叠菜单
		function collapse($tp){
			if(!defaults.isIE6){
				$tp.find('ul:first').slideUp({ duration: defaults.collapseSpeed, easing: defaults.collapseEasing });
			}else{
				$tp.find('ul:first').css('display','none');
			}
		}
		
		//展开菜单
		function expanded($tp){
			if(!defaults.isIE6){
				$tp.find('ul:hidden:first').slideDown({ duration: defaults.expandSpeed, easing: defaults.expandEasing });
			}else{
				$tp.find('ul:hidden:first').css('display','block');
			}
		}
		
		//c: source div, t: url
		function showTree($source, url) {
			$source.addClass('wait');
			
			//获取远程数据
			$.post(defaults.script, { jqueryFiletreeId: url }, function(data) {
				//首次加载时，要去掉一个多余的<ul class="jqueryFileTree start"><li class="wait"></li></ul>
				if(defaults.isFirst){
					$source.find(".jqueryFileTree.start").remove();
					defaults.isFirst = false;
				}
				
				$source.removeClass('wait');
				
				//是否显示叶子(权限)结点，如果不显示，干脆就不添加服务器端返回的数据
				if(!(defaults.showLeaf == false && $(data).find('a[target="mainRight"]').size() > 0)){
					$source.append(data);
				}
				
				//是否显示checkbox
				if(defaults.showCheckbox){
					$source.find(':checkbox').css('display','inline');
				}
				
				//展开结点的孩子
				expanded($source);
				
				var $t1 = $source.find('a[target="mainRight"]');
				//叶子结点的链接href是否有效
				if(!defaults.activeHref){
					$t1.attr('href','#').click(function(){return false;});
				}
				
				//叶子结点不绑定事件
				if($t1.size() > 1){
					return;
				}else{//给非叶子结点绑定单击事件
					bindEventToTree($source);
				}
			});
		}
		
		//绑定事件
		function bindEventToTree($target) {
			$target.find('li a').each(function(){
				$(this).click(function() {//绑定单击事件
					var $p = $(this).parent();
					if( $p.hasClass('directory') ) {//只处理父结点，叶子结点由客户端自己处理
						if( $p.hasClass('collapsed') ) {//当前是折叠状态,则展开
							//如果从来没有加载过孩子，远程获取孩子结点
							if($p.find('ul').size() == 0){
								showTree($p, $(this).attr('rel'));
							}else{
								//否则，直接展开孩子结点
								expanded($p);
							}
							$p.removeClass('collapsed').addClass('expanded');
						} else {//当前是展开状态,则折叠
							collapse($p);
							$p.removeClass('expanded').addClass('collapsed');
						}
					}
					return false;
				});
			});
			
			//是否展开全部父结点
			if(defaults.expandAll){$target.find('li').not('.leaf').find('a').click();}
		}
		
		//深度遍历树，去掉最里层结点的click事件(无论是父结点还是叶子结点)，这样就可以去掉不必要的ajax请求
		function deepIn($m2){
			$m2.find('li').each(function(){
				var $t = $(this).children('ul.jqueryFileTree');
				if($t.size() == 1){
					//所有父结点的checkbox都不能被选中，则直接删除该checkbox
					if(defaults.onlyDeepInCanChecked){
						$(this).children(':checkbox').remove();
					}
					deepIn($t);
				}else{
					//只允许叶子结点(栏目或权限)的checkbox可被选中
					//如果显示叶子结点，则只有权限这样的叶子结点可以被选中
					if(defaults.showLeaf && defaults.onlyDeepInCanChecked){
						if($(this).children('a[target="mainRight"]').size() == 0){
							$(this).children(':checkbox').remove();
						}
					}
					$(this).find('a').unbind('click');
				}
			});
		}
		
		//每0.2秒检测栏目树是否已加载完毕
		var interval = setInterval(function(){
			if($(defaults.id).find('li.wait').size() == 0){
				clearInterval(interval);
				deepIn($(defaults.id));
				
				//执行整树加载完毕后的回调函数
				if(defaults.complete != null)defaults.complete();
				
				//只能选择一个checkbox
				if(defaults.onlyOneCanChecked){
					var $cb = $(defaults.id + ' :checkbox');
					$cb.click(function(){
						$cb.removeAttr('checked');
						$(this).attr('checked','true');
					});
				}
			}
		},200);
		
		// Loading gif and message
		$(this).html('<ul class="jqueryFileTree start"><li class="wait">' + defaults.loadMessage + '<li></ul>');
		
		// Get the initial file list
		showTree($(this), defaults.root);
	};
	
})(jQuery);